package com.alumni_circle.core.utils;

import com.alumni_circle.constant.PathConstant;
import com.alumni_circle.core.config.RedisCache;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.Picture;
import com.alumni_circle.core.service.IPictureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class Upload {

    @Resource
    private IPictureService pictureService;

    @Resource
    RedisCache redisCache;

    public static String getUUID(){
        UUID uuid=UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr=str.replace("-", "");
        return uuidStr;
    }


    @RequestMapping(value = "/upload" )
    @SysLog(type="增",accessInfo="上传图片",url="/upload")
    public String upload(@RequestParam("file") MultipartFile file){

        //若文件选择为空,就上传失败
        if(file.isEmpty()){
            return "上传失败,请选择文件!";
        }
        //获取文件上传的文件名
        String fileName=file.getOriginalFilename();
        //后缀
        assert fileName != null;
        String suffix = fileName.substring(fileName.lastIndexOf("."));

        fileName = fileName.substring(0,fileName.lastIndexOf("."));
        //指定到上传的文件路径
        String filePath = PathConstant.SERVER.getPath();
        if(!new File(filePath+fileName+suffix).exists()){
            File dir = new File(filePath);
            //若路径不存在,则创建一个这样的文件夹
            if (!dir.exists()) {
                dir.mkdir();
            }
            try {
                //创建了一个File对象，名字是file1 ，路径是filePath，名字是fileName。
                File file1 = new File(filePath, fileName+"{-temporary}"+suffix);
                //然后就可以调用这个对象的相关方法完成文件创建，删除，读取，写入等操作
                // 将上传的文件写入创建好的文件中
                file.transferTo(file1);
//                System.out.println(fileName+"{-temporary}"+suffix);
                return (fileName+"{-temporary}"+suffix);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            //寻找不存在的命名
            while (new File(filePath+fileName).exists()){
                fileName = getUUID();
            }
            File dir = new File(filePath);
            //若路径不存在,则创建一个这样的文件夹
            if (!dir.exists()) {
                dir.mkdir();
            }
            try {
                //创建了一个File对象，名字是file1 ，路径是filePath，名字是fileName。
                File file1 = new File(filePath, fileName+"{-temporary}"+suffix);
                //然后就可以调用这个对象的相关方法完成文件创建，删除，读取，写入等操作
                // 将上传的文件写入创建好的文件中
                file.transferTo(file1);
                return (fileName+"{-temporary}"+suffix);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "fail";
    }

    @ResponseBody
    @RequestMapping(value = "/picture",method = RequestMethod.POST)
    @SysLog(type="",accessInfo="",url="/picture")
    public String picture(@RequestBody BaseId baseId){
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id",baseId.getId());
        Picture picture = pictureService.getOne(queryWrapper);
        // 进行浏览量的变化
//        Set<String> set = redisCache.getCacheObject(baseId.getId());
//        set.add(SecurityUtils.getId());
//        int views = set.size();
//        redisCache.setCacheObject(baseId.getId(),set);
        if (picture!=null) {
            return PictureBase64.thumbnailList(picture.getPictures());
        }else {
            return null;
        }
    }
}